package com.wntms.wgj.web;

import com.github.pagehelper.PageInfo;
import com.wntms.gps.pojo.Result;
import com.wntms.wgj.pojo.FreightSettles;
import com.wntms.wgj.service.FreightSettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/freightSettle")
public class FreightSettleController {
    @Autowired
    private FreightSettleService freightSettleService;

    /**
     * 查询所有货运单结算信息
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<FreightSettles> list = freightSettleService.findFreightSettles();
        Result<Object> result = new Result<>();
        if (list == null){
            result.setCode(500);
            result.setMsg("查询失败");
        }else {
            result.setData(list);
            result.setCode(200);
            result.setMsg("查询成功");
        }
        return  result;
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public Result findByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<FreightSettles> list = freightSettleService.findByPage(currentPage,pageSize);

        Result<Object> result = new Result<>();
        if (list != null){
            PageInfo<FreightSettles> pageInfo = new PageInfo<>(list);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    /**
     * 货运单结算
     * @param freightSettles
     * @return
     */
    @PutMapping
    public Result updateSettle(@RequestBody FreightSettles freightSettles){
        boolean flag = freightSettleService.updateSettle(freightSettles);

        Result<Object> result = new Result<>();
        if (!flag){
            result.setCode(500);
            result.setMsg("修改失败");
        }else {
            result.setCode(200);
            result.setMsg("结算成功");
        }

        return result;
    }

    /**
     * 删除结算记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteSettle(@PathVariable Integer id){
        boolean flag = freightSettleService.deleteSettle(id);

        Result<Object> result = new Result<>();
        if (!flag){
            result.setCode(500);
            result.setMsg("删除失败");
        }else {
            result.setCode(200);
            result.setMsg("删除成功");
        }

        return result;
    }

    @GetMapping("/{fid}")
    public Result findByFid(@PathVariable String fid){
        FreightSettles fs = freightSettleService.findByFId(fid);

        Result<Object> result = new Result<>();
        if (fs != null){
            result.setCode(200);
            result.setMsg("查询单条成功");
            result.setData(fs);
        }else {
            result.setCode(200);
            result.setMsg("查询单条失败");
        }

        return result;
    }
}
